package references2bibtexrcp.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

import txt2bib.util.Grammars;
import com.swtdesigner.SWTResourceManager;
/**
 * 
 * @author v
 *
 */
public class SelectGrammarDialog extends Dialog {

	private Composite container;
	Grammars selectedGrammar = null;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SelectGrammarDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		container.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.numColumns = 2;
		
		Label label = new Label(container, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
		label.setText("Selezionare la casa editrice");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		boolean isFirst = true;
		for (Grammars gr: Grammars.values()) {
			if (gr.equals(Grammars.Auto)){} else {
			Button button_2 = new Button(container, SWT.RADIO);
			button_2.setText(gr.toString());
			button_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			button_2.addSelectionListener(new SelectionListener() {
				
				@Override
				public void widgetSelected(SelectionEvent e) {
					selectedGrammar = Grammars.valueOf(((Button)e.getSource()).getText());
				}
				
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
			if (isFirst) {
				button_2.setSelection(true);
				isFirst = false;
				selectedGrammar = gr;
			}}
		}
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	public Grammars getSelectedGrammar() {
		
		for (Control c: container.getChildren()) {
			if (c instanceof Button) 
				if (((Button)c).getSelection()) {
					System.out.println("Get selection");
					return Grammars.valueOf(((Button)c).getText());
				}
		}
		
		return Grammars.Auto;
	}

	@Override
	public int open() {
		// TODO Auto-generated method stub
		return (super.open() >= 0) ? selectedGrammar.value() : -1;
		
	}

	
}
